package com.atguigu.tingshu.album.service.impl;

import com.atguigu.tingshu.album.mapper.BaseCategory1Mapper;
import com.atguigu.tingshu.album.mapper.BaseCategory2Mapper;
import com.atguigu.tingshu.album.mapper.BaseCategory3Mapper;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.model.album.BaseCategory1;
import com.atguigu.tingshu.model.album.BaseCategory2;
import com.atguigu.tingshu.model.album.BaseCategory3;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.vo.album.AlbumCategoryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;


	@Override
	public List<AlbumCategoryVo> getBaseCategoryList() {

		return baseCategory1Mapper.getBaseCategoryList();
	}

	/**
	 * 根据一级分类id查询分类列表
	 * @param category1Id
	 * @return
	 */
	@Override
	public AlbumCategoryVo getBaseCategoryListByOneId(Long category1Id) {
		return baseCategory1Mapper.getBaseCategoryListByOneId(category1Id);
	}

	@Override
	public BaseCategoryView getByCategory3Id(Long category3Id) {
		BaseCategoryView baseCategoryView = baseCategory3Mapper.getByCategory3Id(category3Id);
		return baseCategoryView;
	}

	/**
	 * 根据一级分类id查询置顶的三级分类
	 * @param category1Id
	 * @return
	 */
	@Override
	public List<BaseCategory3> findTopBaseCategory3(Long category1Id) {
		//现根据分类查询出所有的二级分类的id
		List<BaseCategory2> baseCategory2s = baseCategory2Mapper.selectList(new LambdaQueryWrapper<BaseCategory2>().eq(BaseCategory2::getCategory1Id, category1Id));
		List<Long> baseCategory2Ids = baseCategory2s.stream().map(baseCategory2 -> {
			Long id = baseCategory2.getId();
			return id;
		}).toList();
		//并根据二级分类id查询指定的三级分类
		LambdaQueryWrapper<BaseCategory3> baseCategory3LambdaQueryWrapper = new LambdaQueryWrapper<>();
		baseCategory3LambdaQueryWrapper.eq(BaseCategory3::getIsTop, 1);
		baseCategory3LambdaQueryWrapper.in(BaseCategory3::getCategory2Id, baseCategory2Ids);
		baseCategory3LambdaQueryWrapper.last("limit 7");
		List<BaseCategory3> baseCategory3s = baseCategory3Mapper.selectList(baseCategory3LambdaQueryWrapper);

		return baseCategory3s;
	}
}
